package com.yun.zhong.fu.sharecenter.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.yun.zhong.fu.sharecenter.rocketmq.MySource;
import com.yun.zhong.fu.sharecenter.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.security.util.ManifestDigester;


/**
 * @Author RenPu
 * @Date 2020/12/29 15:43
 * @Version 1.0
 * @Description:
 **/

@Slf4j
@RestController
@RequestMapping("/")

//动态更新配置文件，无须重启服务
@RefreshScope
public class TestController {

    @Autowired
    private TestService testService;


    @GetMapping("/testA")
    public String testA() {

        testService.common();
        return "test-A";
    }

    @GetMapping("/testB")
    public String testB() {
        testService.common();
        return "test-B";
    }


//    /**
//     * 测试sentinel的api方法
//     * @param c
//     * @return
//     */
//    @GetMapping("/test-sentinel-api")
//    public String testSentinrlApi(@RequestParam(required = false) String c){
//        Entry entry=null;
//
//        try {
//            entry = SphU.entry("test-sentinel-api");
//            //编写业务逻辑
//            return c;
//        } catch (BlockException e) {
//            log.info("限流，或者降级了");
//            return "限流，或者降级了";
//
//        //此配置用来测试sentinel的异常比例的功能
//        }catch (IllegalArgumentException e){
//            Tracer.trace(e);
//            return "参数非法";
//        }
//        finally {
//            if(entry!=null){
//                //释放资源
//                entry.exit();
//            }
//        }
//    }


    /**
     * 模拟cloud-stream-rocketmq的生产者
     */
    @Autowired
    private Source source;

    @GetMapping("/testStreamRocketMq")
    public String testStreamRocketMq() {
        source.output()
                .send(MessageBuilder
                        .withPayload("我是stream的消息体......")
                        .build());
        return "success";
    }


//    /**
//     * 模拟cloud-stream-rocketmq的生产者
//     */
//    @Autowired
//    private MySource mySource;
//    @GetMapping("/testStreamRocketMqTwo")
//    public String testStreamRocketMqTwo(){
//        mySource.output()
//                .send(MessageBuilder
//                        .withPayload("我是自定义接口stream的消息体......")
//                        .build());
//        return "success";
//    }


    @Value("${your.config}")
    private String yourConfig;

    @GetMapping("/testNacosConfig")
    public String testNacosConfigCenter() {
        return yourConfig;

    }


}
